
package com.roc.web.action.setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.dao.IBaseDao;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction; // import com.roc.pojo.Merchant;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.Operators;
import com.roc.pojo.SettingBean; // import com.roc.pojo.ProfitRule;
import com.roc.service.IPagenitionService; // import
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettflagService;
import com.roc.util.Constance;
import com.roc.util.constant.AuditingResultCodeType;
import com.roc.util.constant.AuditingType;
import com.roc.util.constant.Constant;
import com.roc.util.constant.SysLogType;
import com.roc.web.action.MerchantSearchCondition;

/**
 * @author Feng haitao
 * 
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/setting")
public class UnFrozenAction extends PaginationBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5236170890634487270L;
	private Logger logger = LogManager.getLogger(UnFrozenAction.class);
	private Operators operators;
	private UnFrozenSearchCondition searchCondition;
    private String idd;
	private String msg;
	private SettingBean sett ;
	private AuditingItem auditing;

	@Autowired
	IPagenitionService pagenitionService;

	@Autowired
	IorgService orgService;
	@Autowired
	ISettflagService settflagService;
	@Autowired
	private IBaseDao baseDao;

	// 解冻申请查询
	@Action(value = "unfrozensearch", results = {
			@Result(name = "success", location = "/pages/setting/unfrozenlist.jsp"),
			@Result(name = "error", location = "/pages/setting/unfrozenlist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp") })
	public String unfrozensearch() {
		Operators logingMerchant = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		
		Map<String, Operators> merchantMap = new TreeMap<String, Operators>();
		if (searchCondition == null) {
			searchCondition = (UnFrozenSearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new UnFrozenSearchCondition();
			}
		}
		try {
			// 系统管理员查看全部，代理只能看到本机构下的。
//			if (Constance.MERCHANT_TYPE_DAILI.equals(logingMerchant
//					.getRoleType())) {
//				searchCondition.setOrgCode(logingMerchant.getOrgCode());
//			}
			int rows = pagenitionService.countSQL(searchCondition);
			List<SettingBean> settingList = new ArrayList<SettingBean>();
			if (rows > 0) {
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				if (searchRsList != null && searchRsList.size() > 0) {
					Iterator iterator = searchRsList.iterator();
					while (iterator.hasNext()) {
						Object[] obj = (Object[]) iterator.next();
						SettingBean sett = new SettingBean();
						if (obj[0] != null && !"".equals(obj[0])) {
							sett.setOrderno(obj[0].toString());
						}
						if (obj[1] != null && !"".equals(obj[1])) {
							sett.setMerchantno(obj[1].toString());
						}
						if (obj[2] != null && !"".equals(obj[2])) {
							sett.setMerchantname(obj[2].toString());
						}
						if (obj[3] != null && !"".equals(obj[3])) {
							sett.setSettledate(obj[3].toString());
						}
						if (obj[4] != null && !"".equals(obj[4])) {
							sett.setFreezeflag(obj[4].toString());
						}
						if (obj[5] != null && !"".equals(obj[5])) {
							sett.setTotalamt(Double.valueOf(obj[5].toString()));
						}
						if (obj[6] != null && !"".equals(obj[6])) {
							sett
									.setTotalnum(Integer.valueOf(obj[6]
											.toString()));
						}
						if (obj[7] != null && !"".equals(obj[7])) {
							sett.setMerchantfee(Double.valueOf(obj[7]
									.toString()));
						}
						if (obj[8] != null && !"".equals(obj[8])) {
							sett.setMerchantsettleamt(Double.valueOf(obj[8]
									.toString()));
						}
						if (obj[9] != null && !"".equals(obj[9])) {
							sett.setSettleflag(obj[9].toString());
						}
						if (obj[10] != null && !"".equals(obj[10])) {
							sett.setBatchno(obj[10].toString());
						}
						if (obj[11] != null && !"".equals(obj[11])) {
							sett.setId(Long.valueOf(obj[11].toString()));
						}
						settingList.add(sett);
					}
				}
			}
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute("settingList", settingList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);

		} catch (ServiceException e) {

			logger.error(e.getMessage(), e);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);

		}
		return SUCCESS;
	}

	// 解冻申请初始页面
	@Action(value = "unfrozenlist", results = {
			@Result(name = "success", location = "/pages/setting/unfrozenlist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp"),
			@Result(name = "error", location = "/error.jsp") }

	)
	public String unfrozenlist() throws Exception {
		// List settingflagList=new ArrayList();
		// settingflagList=settflagService.querySettingFlagList();
		// getRequest().setAttribute("settingflagList", settingflagList);
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (UnFrozenSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_BEAN_UNFROZEN);
	}

	

	@Action(value = "unfrozenapply", results = { @Result(name = "success", location = "/pages/setting/unfrozenapply.jsp")
            })
	//查询解冻详细信息
	public String unfrozenapply(){
//		
//		List<SettingBean> settingList= (List<SettingBean>)getSession().getAttribute("settingList");
//			for(int i=0;i<settingList.size();i++){
//				SettingBean SettingBean = settingList.get(i);
//				if(Long.valueOf(idd)==SettingBean.getId()){
//					sett = SettingBean;
//				}
//			}	
		try {
			sett = settflagService.querySettingBean(idd);
		
			Map auditingMap=new HashMap();
			auditingMap.put(AuditingResultCodeType.UNFREEZEAPPLY.getCode(),AuditingResultCodeType.UNFREEZEAPPLY.getName());
			getRequest().setAttribute("auditingMap",auditingMap);
			getRequest().setAttribute("sett", sett);
			//历史审核结果。
			List<AuditingItem>  list = new ArrayList<AuditingItem>();
			list= settflagService.queryAuditingItemList(idd);
			getRequest().setAttribute("list", list);
			
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	//解冻操作
	@Action(value = "unfrozenupdate", results = { @Result(name = "success", location = "/pages/setting/unfrozenlist.jsp")
             })
	public String unfrozenupdate() throws Exception{
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		//调用service层
		try {
				//编辑审核结果
				auditing.setAudit_code(AuditingType.SETTING_UNFROZENAPPLY.getCode());
				auditing.setAudit_name(AuditingType.SETTING_UNFROZENAPPLY.getName());
				auditing.setObj_Audited(idd);
				auditing.setOperators(logingMerchant.getAccountNo());
				//auditing.setResult_code(auditing.getAudit_code());
				//auditing.setResult_detail(auditing.getResult_detail());
				sett = settflagService.querySettingBean(idd);
				//当界面选择同意并且和枚举中的同意code相同时进行冻结。
				String stuts = "";
				if(auditing.getResult_code().equals(AuditingResultCodeType.UNFREEZEAPPLY.getCode())){
					sett.setFreezeflag(AuditingResultCodeType.UNFREEZEAPPLY.getCode());  //申请解冻
					stuts= AuditingResultCodeType.UNFREEZEAPPLY.getCode();
				}
				
				
				boolean b =	settflagService.frozenApply(auditing, sett,stuts);
				if(b){
					addActionMessage("审核成功");
				}else{
					addActionMessage("审核失败");
				}
					
					logger.info("------------------------------操作员【"+logingMerchant.getAccountNo()+"】申请冻结【"+idd+"】成功！-----------------------------------");
				} catch (ServiceException e) {
					logger.info("------------------------------操作员【"+logingMerchant.getAccountNo()+"】申请冻结【"+idd+"】失败！-----------------------------------");
					logger.error(e.getMessage(),e);
					return ERROR;
				}
				return unfrozensearch();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_UNFROZEN, searchCondition);
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}


	/**
	 * @return the profitModelHead
	 */

	public Operators getOperators() {
		return operators;
	}

	public void setOperators(Operators operators) {
		this.operators = operators;
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return null;
	}

	public UnFrozenSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(UnFrozenSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public String getIdd() {
		return idd;
	}

	public void setIdd(String idd) {
		this.idd = idd;
	}

	public AuditingItem getAuditing() {
		return auditing;
	}

	public void setAuditing(AuditingItem auditing) {
		this.auditing = auditing;
	}

	public SettingBean getSett() {
		return sett;
	}

	public void setSett(SettingBean sett) {
		this.sett = sett;
	}



}
